package org.gikolet.ch2.android.viewer;

import java.io.IOException;

import org.gikolet.ch2.android.R;
import org.gikolet.ch2.android.widget.ThreadHeaderAdapter;
import org.gikolet.ch2.data.Board;
import org.gikolet.ch2.data.ThreadHeader;
import org.gikolet.ch2.logic.ThreadHeadersLogic;

import roboguice.inject.ContextScoped;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.google.inject.Inject;
import com.google.inject.Injector;

@ContextScoped
public class ThreadHeadersViewer extends Ch2Viewer {

	private static final int MENU_SORT_THREAD_HEADERS = 0;

	private static final int MENU_THREAD_BUILD = 1;

	private ListView subjectsView;

	@Inject
	private Injector injector;

	@Inject
	private ResesViewer resesViewer;

	@Inject
	private ThreadHeadersLogic subjectsLogic;

	@Inject
	private Context context;

	private ThreadHeaderAdapter subjectsAdapter;

	public ThreadHeadersViewer() {
	}

	public void createView(ViewGroup parent) {

		this.subjectsView = new ListView(parent.getContext());

		TextView emptyView = (TextView) parent.findViewById(R.id.ThreadHeaderEmpty);
		System.out.println(emptyView);
		this.subjectsView.setEmptyView(emptyView);

		this.subjectsAdapter = new ThreadHeaderAdapter();
		this.injector.injectMembers(this.subjectsAdapter);

		this.subjectsView.setFastScrollEnabled(true);
		this.subjectsView.setAdapter(this.subjectsAdapter);

		this.subjectsView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				ThreadHeader s = (ThreadHeader) parent
						.getItemAtPosition(position);

				resesViewer.load(s, true, true);
			}
		});

		parent.addView(this.subjectsView);
	}

	@Override
	public boolean createMenu(Menu menu) {
		// メニューの追加
		MenuItem sortThreadHeadersMenu = menu.add(0, MENU_SORT_THREAD_HEADERS,
				0, R.string.MENU_SORT_THREAD_HEADERS);
		MenuItem threadBuildMenu = menu.add(0, MENU_THREAD_BUILD, 0,
				R.string.MENU_THREAD_BUILD);

		sortThreadHeadersMenu.setIcon(android.R.drawable.ic_menu_sort_by_size);
		threadBuildMenu.setIcon(android.R.drawable.ic_menu_myplaces);

		super.createMenu(menu);

		return true;
	}

	@Override
	public boolean onMenuItemSelected(MenuItem item) {
		if (super.onMenuItemSelected(item)) {
			return true;
		}
		switch (item.getItemId()) {

		case MENU_SORT_THREAD_HEADERS:
			sortThreadHeaders();
			return true;

		case MENU_THREAD_BUILD:
			threadBuild();
			return true;

		default:
			return false;
		}
	}

	private void sortThreadHeaders() {
		new AlertDialog.Builder(this.context).setTitle("並び替え")
				.setMessage("未実装").setPositiveButton("OK", null).show();
	}

	private void threadBuild() {
		new AlertDialog.Builder(this.context).setTitle("スレ立て")
				.setMessage("未実装").setPositiveButton("OK", null).show();
	}

	public void load(Board board, boolean cache) {
		if (!cache) {
			UpdateTask task = new UpdateTask();

			task.execute(board);

		} else {
			ThreadHeadersViewer.this.subjectsAdapter.setBoard(board);
		}

		requestView();
	}

	@Override
	public void saveInstanceState(Bundle outState) {
	}

	@Override
	public void restoreInstanceState(Bundle savedInstanceState) {
	}

	class UpdateTask extends AsyncTask<Board, Integer, Board> implements
			OnCancelListener {

		ProgressDialog progressDialog;
		IOException ioe;

		@Override
		protected Board doInBackground(Board... params) {

			try {

				// reader =
				// subjectsReaderProvider.createSubjectReader(params[0]);
				//
				// // ThreadHeader threadHeader;
				// while ((/* threadHeader = */reader.read()) != null
				// && !isCancelled()) {
				// // threadHeaderList.add(threadHeader);
				// }
				//
				// if (isCancelled()) {
				// // threadHeaderList = null;
				// }
				subjectsLogic.update(params[0], true);

				return params[0];

			} catch (IOException e) {
				this.ioe = e;
				return null;
			}
		}

		@Override
		protected void onPreExecute() {
			progressDialog = new ProgressDialog(
					ThreadHeadersViewer.this.subjectsView.getContext());

			progressDialog.setTitle("スレッド一覧更新中");
			progressDialog.setMessage("しばらくお待ちください");
			progressDialog.setIndeterminate(true);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setCancelable(true);
			progressDialog.setOnCancelListener(this);

			progressDialog.show();
		}

		public void onCancel(DialogInterface dialog) {
			cancel(true);
		}

		@Override
		protected void onPostExecute(Board result) {
			if (ioe == null) {
				ThreadHeadersViewer.this.subjectsAdapter.setBoard(result);
			} else {
				ioe.printStackTrace();
			}
			progressDialog.dismiss();
		}
	}
}
